using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioPlay : MonoBehaviour
{
    public AudioClip backgroundAudio;
    public AudioClip playerDieAudioClip;
    private AudioSource backgroundAudioSource;
    private AudioSource dieAudioSource;
    private Player player;
    private bool hasPlayedDeathAudio = false; // 用于记录音效是否已经播放

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        backgroundAudioSource = gameObject.AddComponent<AudioSource>();
        dieAudioSource = gameObject.AddComponent<AudioSource>();

        backgroundAudioSource.clip = backgroundAudio;
        backgroundAudioSource.loop = true; // 循环播放背景音乐
        backgroundAudioSource.Play(); // 开始播放背景音乐

        dieAudioSource.clip = playerDieAudioClip;
    }

    void Update()
    {
        if (player.stats.health <= 0 && !hasPlayedDeathAudio)
        {
            backgroundAudioSource.Stop(); // 停止背景音乐
            dieAudioSource.Play(); // 播放死亡音效
            hasPlayedDeathAudio = true; // 设置音效已播放
        }
    }
}